package com.spark.common.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.coolindicator.sdk.CoolIndicator
import com.just.agentweb.AgentWeb
import com.just.agentweb.AgentWebUtils
import com.just.agentweb.BaseIndicatorView
import com.spark.common.BaseActivity
import com.spark.common.R
import kotlinx.android.synthetic.main.act_web.*

class WebActivity : BaseActivity() {
    private lateinit var agentWeb: AgentWeb

    companion object {
        fun actionStart(host: Activity, title: String = "", url: String = "") {
            val intent = Intent(host, WebActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("url", url)
            host.startActivity(intent)
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_web
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        intent?.apply {
            val title = getStringExtra("title")
            val url = getStringExtra("url")

            tv_title.text = title

            iv_back.setOnClickListener { finish() }

            val blinkIndicator = BlinkIndicator(this@WebActivity)
            agentWeb = AgentWeb.with(this@WebActivity)
                    .setAgentWebParent(ll_web, LinearLayout.LayoutParams(-1, -1))
                    .setCustomIndicator(blinkIndicator)
                    .interceptUnkownUrl()
                    .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                    .createAgentWeb()
                    .ready()
                    .go(url)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (agentWeb.back()){
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onResume() {
        agentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onPause() {
        agentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        agentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
    }
}

/**
 * progress indicator looks like bling bling
 */
class BlinkIndicator(context: Context, attrs: AttributeSet?, defStyle: Int = -1) : BaseIndicatorView(context, attrs, defStyle) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, -1)
    constructor(context: Context) : this(context, null, -1)

    private var coolIndicator = CoolIndicator.create(context as Activity)

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            coolIndicator.progressDrawable = resources.getDrawable(R.drawable.default_drawable_indicator, context.theme)
        } else {
            coolIndicator.progressDrawable = resources.getDrawable(R.drawable.default_drawable_indicator)
        }
        addView(coolIndicator, offerLayoutParams())
    }

    override fun show() {
        this.visibility = View.VISIBLE
        coolIndicator.start()
    }

    override fun hide() {
        coolIndicator.complete()
    }

    override fun offerLayoutParams(): LayoutParams {
        return FrameLayout.LayoutParams(-1, AgentWebUtils.dp2px(context, 3f))
    }
}